// myMixin.js
import { getFinishProductDetail,getOrderDetail } from "@/api/product/index";
export const myMixin = {
  data() {
    return {
      dayinlist: [],
      dayinlists: [],
    };
  },
  created() {},
  methods: {
    // 表格数据回显
    toggleSelection(rows) {
      //使用$nextTick，保证dom已经更新
      this.$nextTick(() => {
        setTimeout(() => {
          //保证有值再进行比对，避免报错
          if (rows && this.dataList.length != 0) {
            //遍历table表格数据与要回显的数组比对，匹配成功的设置为选中状态
            this.dataList.forEach((row) => {
              rows.forEach((item) => {
                if (row.id == item.id) {
                  //判断唯一标识，若是id一样，则设置该行选中状态为true
                  this.$refs.multipleTable.toggleRowSelection(row, true);
                }
              });
            });
          } else {
            this.$refs.multipleTable.clearSelection();
          }
        }, 0);
      });
    },
    //列表唯一id
    getRowKeys(row) {
      return row.id;
    },
    clearSelection(){
      this.$refs.multipleTable.clearSelection();
    },
    set_products(id) {
      let obj = {};
      getFinishProductDetail({ finish_product_id: id }).then((res) => {
        if (res.code == 200) {
          this.isShow_product = true;
          obj = res.data[0];
        }
      });
      return obj;
    },
    dayin_liucheng() {
      if (this.dayinlist.length > 0) {
        const routerurl = this.$router.resolve({
          name: "dayin_liucheng",
          path: "/dayin_liucheng",
          query: {
            data: JSON.stringify(this.dayinlist),
          },
        });
        window.open(routerurl.href, "_blank");
        this.clearSelection()
      } else {
        this.$message({
          type: "error",
          message: "请勾选数据",
        });
        return false;
      }
    },
    Dayin_biaoqian() {
      if (this.dayinlist.length == 1) {
        const routerurl = this.$router.resolve({
          name: "biaoqian",
          path: "/biaoqian",
          query: {
            data: JSON.stringify(this.dayinlist),
          },
        });
        window.open(routerurl.href, "_blank");
        this.clearSelection()
      } else {
        if (this.dayinlist.length == 0) {
          this.$message({
            type: "error",
            message: "请勾选数据",
          });
          return false;
        }
        if (this.dayinlist_biaoqian.length > 1) {
          this.$message({
            type: "error",
            message: "只能选择一条数据",
          });
          return false;
        }
      }
    },
    Dayin_renwu() {
      if (this.dayinlist.length > 0) {
        const routerurl = this.$router.resolve({
          name: "dayin_renwu",
          path: "/dayin_renwu",
          query: {
            data: JSON.stringify(this.dayinlist),
          },
        });
        window.open(routerurl.href, "_blank");
        this.clearSelection()
      } else {
        this.$message({
          type: "error",
          message: "请勾选数据",
        });
        return false;
      }
    },
    //打印出货单
    Dayin_chuhuo(type) {
      // 订单列表
      if (type === 0) {
        if (this.dayinlist.length > 0) {
          const fetchOrderDetails = (id) => getOrderDetail({ id });
          const promises = this.dayinlist.map((el) => fetchOrderDetails(el.id));
          Promise.all(promises).then((results) => {
            const validResults = results.filter((res) => res.code === 200);
            const orderDetails = validResults.map((res) => res.data[0]);
    
            const routerUrl = this.$router.resolve({
              name: "dayin_chuhuo",
              query: {
                data: JSON.stringify(orderDetails),
              },
            });
            window.open(routerUrl.href, "_blank");
            this.clearSelection()
          }).catch((error) => {
            console.error("Error fetching order details:", error);
            this.$message({
              type: "error",
              message: "获取订单详情失败",
            });
          });
        } else {
          this.$message({
            type: "error",
            message: "请勾选数据",
          });
        }
      }else{
        if (this.dayinlist.length > 0) {
          const fetchOrderDetails = (id) => getOrderDetail({id});
          const promises = this.dayinlist.map((el) => fetchOrderDetails(el.order_id));
          Promise.all(promises).then((results) => {
            const validResults = results.filter((res) => res.code === 200);
            const orderDetails = validResults.map((res) => res.data[0]);
    
            const routerUrl = this.$router.resolve({
              name: "dayin_chuhuo",
              query: {
                data: JSON.stringify(orderDetails),
              },
            });
            window.open(routerUrl.href, "_blank");
            this.clearSelection()
          }).catch((error) => {
            console.error("Error fetching order details:", error);
            this.$message({
              type: "error",
              message: "获取订单详情失败",
            });
          });
        } else {
          this.$message({
            type: "error",
            message: "请勾选数据",
          });
        }
      }
    
    },
  },
};
